import 'package:distribution_mall_flutter/constant/constant.dart';
import 'package:distribution_mall_flutter/dialog/delete_account_check_dialog.dart';
import 'package:distribution_mall_flutter/entity/delete_account/delete_account_check_entity.dart';
import 'package:distribution_mall_flutter/http/modules/delete_account.dart';
import 'package:distribution_mall_flutter/router/router.gr.dart';
import 'package:distribution_mall_flutter/utils/storage.dart';
import 'package:flutter/material.dart';
import 'package:riverpod/riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'delete_account.g.dart';

@riverpod
class DeleteAccountCheck extends _$DeleteAccountCheck {
  @override
  Future<DeleteAccountCheckBody?> build() async {
    var data = (await DeleteAccountApi.check()).data;
    return data;
  }

  void checkAccount(BuildContext context) {
    bool isPhone = false;
    // 检查是否有 手机 或 邮件
    // state.value!.pass = false;
    var deleteAccountCheckBody = state.value!;
    if (deleteAccountCheckBody.pass) {
      if (deleteAccountCheckBody.primaryAccount.accountType ==
          ReceiverType.phoneNumber) {
        isPhone = true;
      } else {
        isPhone = false;
      }
      appRouter.replace(DeleteAccountVerifyRoute(
          isPhone: isPhone,
          accountType: deleteAccountCheckBody.primaryAccount.accountType,
          accountName: deleteAccountCheckBody.primaryAccount.accountName));
    } else {
      String content = '', path = '', confirmText = 'OK';
      bool hideCloseBtn = false;
      deleteAccountCheckBody.checklist.every((v) {
        // content =
        // 'There are still orders in progress in your account. Please confirm that the order is completed before reapplying.';
        // path = 'home';
        // confirmText = 'Return to Home';
        // hideCloseBtn = true;
        // return false;
        if (v.item == 'UNFINISHED_ORDERS') {
          content =
              'There are still orders in progress in your account. Please confirm that the order is completed before reapplying.';
          path = 'home';
          confirmText = 'Return to Home';
          hideCloseBtn = true;
          return v.pass;
        }
        if (v.item == 'PRIMARY_ACCOUNT') {
          content =
              'Sorry, your account does not have a main account. Please add a main account and then apply again.';
          path = 'bindPhone';
          confirmText = 'Add Account';
          return v.pass;
        }
        return false;
      });
      showDialog(
          context: context,
          builder: (_) => DeleteAccountCheckDialog(
              content: content, confirmText: confirmText, path: path));
    }
  }
}
